export default {
    data() {
        return {
            brokenType: null, // 故障类型
            brokenText: ['扫码后点击开门，打不开门', '门关不上', '其他'],
            imageList: [], // 图片列表
            fileKey: '', // 上传文件id
        }
    },
    methods: {

        // 类型选择
        typeSelect: function(options) {
            console.log(options)
            let type = options.detail.value;
            this.brokenType = type;
        },

        // 选择图片
        pickerImage: function() {
            console.log("进来了啊")
            uni.chooseImage({
                count: 3, //默认9
                sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], //从相册选择
                success: (res) => {
                    let list = res.tempFilePaths;
                    console.log("进来2")
                    // #ifdef MP-ALIPAY
                    console.log("支付宝上传图片")
                    for (let i = 0; i < list.length; i++) {
                        this.uploadImage(list[i]);
                    }
                    // #endif

                    // #ifdef MP-WEIXIN
                    console.log("微信")
                    var FSM = uni.getFileSystemManager();

                    for (let i = 0; i < list.length; i++) {
                        FSM.readFile({
                            filePath: list[i],
                            encoding: "base64",
                            success: (res) => {
                                let base64Data = 'data:image/jpeg;base64,' + res.data;
                                this.imageList.push(base64Data)
                            },
                            complete: (res) => {
                                uni.showModal({
                                    title: '提示',
                                    content: res.errorMessage,
                                    showCancel: false,
                                });
                            }
                        });
                    }
                    // #endif
                }
            });
        },

        // 上传图片
        uploadImage: function(url) {
            uni.showLoading({
                title: '正在上传图片'
            })
            uni.uploadFile({
                url: this.$api.AliUploadImage,
                filePath: url,
                name: 'file',
                formData: {},
                fileType: 'image',
                success: (res) => {
                    uni.hideLoading();
                    res = JSON.parse(res.data);
                    console.log(res)
                    if (res.returnCode == "SUCCESS") {
                        this.imageList.push(url);
                        uni.showToast({
                            title: res.returnMessage,
                        });
                        this.fileKey = JSON.parse(res.data).fileKey;
                    } else {
                        uni.showToast({
                            title: res.returnMessage,
                            icon: 'none'
                        });
                    }
                }
            });
        },

        // 提交
        push: function() {
            if (this.brokenType == null || this.brokenType == undefined) {
                uni.showToast({
                    title: '请选择故障类型',
                    icon: "none",
                });
                return false;
            }
            let pic = '[';
            this.imageList.forEach((item, index) => {
                if (index + 1 == this.imageList.length) {
                    pic += '"' + item + '"]';
                } else {
                    pic += '"' + item + '",';
                }
            })
            // #ifdef MP-ALIPAY
            let data = {
                userId: this.$store.state.userInfo.userId,
                deviceId: this.$store.state.deviceId,
                problem: this.brokenText[this.brokenType],
                fileKey: this.fileKey
            }
            let url = this.$api.AliPushBroken
            // #endif

            // #ifdef MP-WEIXIN
            let data = {
                userId: this.$store.state.userInfo.userId,
                deviceId: this.$store.state.deviceId,
                problem: this.brokenText[this.brokenType],
                pic: JSON.stringify(this.imageList),

            }
            let url = this.$api.PushBroken
            // #endif


            this.$request(url, data, "POST").then(res => {
                if (res.returnCode == 'SUCCESS') {
                    console.log(res)
                    uni.showModal({
                        title: '提示',
                        content: res.returnMessage,
                        showCancel: false,
                        success: (res) => {
                            uni.navigateBack();
                        }
                    })
                } else {
                    uni.showToast({
                        title: res.returnMessage,
                        icon: 'none'
                    });
                }
            })
        },
    }
}